package org.sam.beatles.ui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableCellRenderer;

import org.sam.swing.ComponentSearch;
import org.sam.swing.JSToolBar;
import org.sam.swing.resource.ResourceLoader;
import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.defaultImpl.JSTableArrayBuilderImpl;
import org.sam.swing.table.renderer.JSTableRowNumberRenderer;
import org.sam.swing.table.tools.JSTableToolBar;

/**
 * 日志窗口部分 升级为表格形式
 * 
 * @author sam
 *
 */
public class JPanelLog extends JPanelSingleDataWindow<Object[]> implements ComponentSearch {

	private static final long serialVersionUID = 5570768906438485886L;

	/**
	 * 日志窗口部分
	 * 
	 * @param frameId
	 * @param title
	 */
	public JPanelLog(String frameId, String title) {
		this.buildCompents();
	}

	/**
	 * 日志窗口
	 */
	public JPanelLog() {
		this("logWindow", "日志窗口");
	}

	@Override
	protected JSTable buildTable() {
		DefaultTableCellRenderer renderl = new DefaultTableCellRenderer();
		renderl.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);

		JSTableColumn c0 = new JSTableColumn();
		c0.setHeaderValue("#");
		c0.setTitle("#");
		c0.setIdentifier(0);
		c0.setCellRenderer(new JSTableRowNumberRenderer());
		c0.setEditable(false);
		c0.setWidth(15);
		c0.setModelIndex(0);

		JSTableColumn c0_1 = new JSTableColumn();
		c0_1.setHeaderValue("类别");
		c0_1.setTitle("类别");
		c0_1.setIdentifier(1);
		c0_1.setCellRenderer(renderl);
		c0_1.setEditable(false);
		c0_1.setMinWidth(75);
		c0_1.setDefaultValue(null);

		JSTableColumn c1 = new JSTableColumn();
		c1.setHeaderValue("等级");
		c1.setTitle("等级");
		c1.setIdentifier(2);
		c1.setCellRenderer(renderl);
		c1.setEditable(false);
		c1.setMinWidth(75);
		c1.setDefaultValue(null);

		JSTableColumn c2 = new JSTableColumn();
		c2.setHeaderValue("日志");
		c2.setTitle("日志");
		c2.setIdentifier(3);
		c2.setCellRenderer(renderl);
		c2.setEditable(false);
		c2.setMinWidth(375);
		c2.setDefaultValue(null);

		try {

			JSTableBuilder<Collection<Object[]>> builder = new JSTableArrayBuilderImpl(c0, c0_1, c1, c2);
			this.tableModel = builder.buildTableModel();
			this.tableColModel = builder.buildTableColumnModel();
			this.table = builder.buildTable();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return this.table;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected JSToolBar buildToolBar() {
		JSTableToolBar toolBar = new JSTableToolBar(table);
		AbstractAction action = new AbstractAction() {

			private static final long serialVersionUID = 1582678062869350040L;

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					tableModel.clear();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		};
		action.putValue(Action.SMALL_ICON, new ImageIcon(ResourceLoader.getResource("no.png")));
		toolBar.addAction(action);

		toolBar.enableDel();
		return toolBar;
	}

	/**
	 * 实现查找组件的方法
	 */
	@Override
	public Component[] getAWTComponents() {
		return this.getComponents();
	}

}
